home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / visulztn / saoimage / saoimage.lha / clrhard.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  7KB  |  219 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    clrhard.c (Color Hard)
  6.  * Purpose:    Get common named colors
  7.  * Subroutine:    init_hard_colors()        returns: void
  8.  *        lookup_cursor_colors()        returns: void
  9.  *        alloc_cursor_cell_color()    returns: int
  10.  *        free_cursor_cell_color()    returns: void
  11.  * Xlib calls:    XAllocNamedColor(), XAllocColor()
  12.  * Xlib calls:    XFreeColors(), XLookupColor()
  13.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  14.  *        You may do anything you like with this file except remove
  15.  *        this copyright.  The Smithsonian Astrophysical Observatory
  16.  *        makes no representations about the suitability of this
  17.  *        software for any purpose.  It is provided "as is" without
  18.  *        express or implied warranty.
  19.  * Modified:    {0} Michael VanHilst    initial version           9 May 1989
  20.  *        {n} <who> -- <does what> -- <when>
  21.  */
  22.  
  23. #include <stdio.h>            /* stderr, FILE, NULL, etc. */
  24. #include <X11/Xlib.h>            /* X window stuff */
  25. #include <X11/Xutil.h>            /* X window manager stuff */
  26. #include "hfiles/color.h"        /* color structs */
  27.  
  28. /*
  29.  * Subroutine:    init_hard_colors
  30.  * Purpose:    Set up basic hardware colors
  31.  */
  32. void init_hard_colors ( color, colormap )
  33.      struct colorRec *color;
  34.      Colormap colormap;
  35. {
  36.   static int get_hard_color();
  37.  
  38.   color->hard.red =
  39.     get_hard_color(color->display, colormap, "red", 63000, 0, 0);
  40.   color->gcset.red.foreground = color->hard.red;
  41.   color->hard.green =
  42.     get_hard_color(color->display, colormap, "green", 0, 60000, 0);
  43.   color->gcset.green.foreground = color->hard.green;
  44.   color->hard.blue =
  45.     get_hard_color(color->display, colormap, "blue", 0, 0, 65535);
  46.   color->gcset.blue.foreground = color->hard.blue;
  47.   color->hard.yellow =
  48.     get_hard_color(color->display, colormap, "yellow", 65535, 65535, 0);
  49.   color->hard.true_black =
  50.     get_hard_color(color->display, colormap, "black", 0, 0, 0);
  51.   color->gcset.black.foreground = color->hard.true_black;
  52.   color->hard.true_white =
  53.     get_hard_color(color->display, colormap, "white", 65535, 65535, 65535);
  54.   color->gcset.white.foreground = color->hard.std_white;
  55. }
  56.  
  57. /*
  58.  * Subroutine:    lookup_cursor_colors
  59.  * Purpose:    Get color parameters from named cursor colors
  60.  */
  61. void lookup_cursor_colors ( color, colormap, init )
  62.      struct colorRec *color;
  63.      Colormap colormap;
  64.      int init;
  65. {
  66.   static void lookup_color();
  67.  
  68.   /* update xcolor structs if needed */
  69.   if( init || (color->cur.desired_cur != NULL) ) {
  70.     lookup_color(color->display, colormap, &(color->cur.color_cur),
  71.           color->cur.desired_cur, color->cur.default_cur);
  72.     color->cur.desired_cur = NULL;
  73.   }
  74.   if( init || (color->cur.desired_one != NULL) ) {
  75.     lookup_color(color->display, colormap, &(color->cur.color_one),
  76.           color->cur.desired_one, color->cur.default_one);
  77.     color->cur.desired_one = NULL;
  78.   }
  79.   if( init || (color->cur.desired_two != NULL) ) {
  80.     lookup_color(color->display, colormap, &(color->cur.color_two),
  81.           color->cur.desired_two, color->cur.default_two);
  82.     color->cur.desired_two = NULL;
  83.   }
  84. }
  85.  
  86. /*
  87.  * Subroutine:    alloc_cursor_cell_color
  88.  * Purpose:    Allocate defined colors for cursors and overlays
  89.  * Returns:    Pixel value for the cursor color
  90.  * Pre-state:    XColor for cur, one, and two, initialized
  91.  * Post-state:    Pixel values for one and two set, that for cur returned.
  92.  */
  93. int alloc_cursor_cell_color ( color, colormap )
  94.      struct colorRec *color;
  95.      Colormap colormap;
  96. {
  97.   int val;
  98.   static int alloc_hard_color();
  99.  
  100.   if( (color->cur.disp_one =
  101.       alloc_hard_color(color->display, colormap, &color->cur.color_one)) < 0 )
  102.     color->cur.disp_one = color->hard.blue;
  103.   if( (color->cur.disp_two =
  104.       alloc_hard_color(color->display, colormap, &color->cur.color_two)) < 0 )
  105.     color->cur.disp_two = color->hard.red;
  106.   if( (val =
  107.       alloc_hard_color(color->display, colormap, &color->cur.color_cur)) < 0 )
  108.     val = color->hard.green;
  109.   return( val );
  110. }
  111.  
  112. /*
  113.  * Subroutine:    free_cursor_cell_color
  114.  * Purpose:    Free cell color overlay colors
  115.  */
  116. void free_cursor_cell_color ( color )
  117.      struct colorRec *color;
  118. {
  119.   static void free_readonly_color();
  120.  
  121.   free_readonly_color(color, (int)color->cur.color_one.pixel);
  122.   free_readonly_color(color, (int)color->cur.color_two.pixel);
  123.   free_readonly_color(color, (int)color->cur.color_cur.pixel);
  124. }
  125.  
  126. /*
  127.  * Subroutine:    free_readonly_color
  128.  * Purpose:    Free cursor color if it is not one of the essential ones
  129.  * Xlib calls:    XFreeColors()
  130.  */
  131. static void free_readonly_color ( color, pixel_value )
  132.      struct colorRec *color;
  133.      int pixel_value;
  134. {
  135.   if( (pixel_value != color->hard.red) &&
  136.       (pixel_value != color->hard.green) &&
  137.       (pixel_value != color->hard.blue) &&
  138.       (pixel_value != color->hard.true_black) &&
  139.       (pixel_value != color->hard.true_white) &&
  140.       (pixel_value != color->hard.std_black) &&
  141.       (pixel_value != color->hard.std_white) )
  142.     XFreeColors(color->display, color->colormap,
  143.         (unsigned long *)&pixel_value, 1, 0);
  144. }
  145.  
  146. /*
  147.  * Subroutine:    get_hard_color
  148.  * Purpose:    Alloc a read-only color cell with a common color
  149.  * Xlib calls:    XAllocNamedColor(), XAllocColor()
  150.  * Method:    Get a color by name if possible, else reserve it by value
  151.  */
  152. static int get_hard_color ( display, colormap, name, red, green, blue )
  153.      Display *display;
  154.      Colormap colormap;
  155.      char *name;
  156.      int red, green, blue;
  157. {
  158.   XColor actual, ideal;
  159.  
  160.   /* see if we can get a standard named color */
  161.   if( XAllocNamedColor(display, colormap, name, &actual, &ideal) == 0 ) {
  162.     /* else reserve our own */
  163.     actual.pixel = 0;
  164.     actual.red = red;
  165.     actual.green = green;
  166.     actual.blue = blue;
  167.     if( XAllocColor(display, colormap, &actual) == 0 ) {
  168.       (void)fprintf(stderr, "WARNING: could not get harware color.\n");
  169.       return( 0 );
  170.     }
  171.   }
  172.   return( (int)actual.pixel );
  173. }
  174.  
  175. /*
  176.  * Subroutine:    lookup_color
  177.  * Purpose:    Set up the rgb values in the Xcolor
  178.  * Xlib calls:    XLookupColor
  179.  * Method:    Use the desired color if given and known to Xlib, else use
  180.  *        the defaut color.
  181.  */
  182. static void lookup_color ( display, colormap, xcolor,
  183.                desired_name, default_name )
  184.      Display *display;
  185.      Colormap colormap;
  186.      XColor *xcolor;
  187.      char *desired_name;
  188.      char *default_name;
  189. {
  190.   XColor ideal;
  191.  
  192.   if( desired_name != NULL ) {
  193.     if( XLookupColor(display, colormap, desired_name, &ideal, xcolor) ) {
  194.       return;
  195.     } else {
  196.       (void)fprintf(stderr,"WARNING: Could not find color: %s, using: %s\n",
  197.             desired_name, default_name);
  198.     }
  199.   }
  200.   XLookupColor(display, colormap, default_name, &ideal, xcolor);
  201. }
  202.  
  203. /*
  204.  * Subroutine:    alloc_hard_color
  205.  * Purpose:    Given rgb values, set the pixel
  206.  * Xlib calls:    XAllocColor()
  207.  * Returns:    Pixel value used or -1 if failed
  208.  */
  209. static int alloc_hard_color ( display, colormap, xcolor )
  210.      Display *display;
  211.      Colormap colormap;
  212.      XColor *xcolor;
  213. {
  214.   if( XAllocColor(display, colormap, xcolor) )
  215.     return( (int)xcolor->pixel );
  216.   else
  217.     return( -1 );
  218. }
  219.